<?php


namespace app\admin\service\control;

use app\admin\model\BaseModel;
use app\admin\model\control\GameStock;
use app\admin\model\platform\GameKindItem;
use app\admin\model\platform\GameRoomInfo;
use app\admin\model\record\RecordModificationStock;
use app\admin\service\BaseService;
use app\admin\service\CommonService;
use think\facade\Cache;
use think\facade\Db;

/**
 * @author tangsan
 * @package app\admin\service
 */
class GameStockService extends BaseService
{
    /**
     * 构造函数
     * @author tangsan
     * ActionLogService constructor.
     */
    public function __construct()
    {
        $this->model = new GameStock();
    }

    /**
     * 获取数据列表
     * @return array
     * @author tangsan
     */
    public function getList()
    {
        $param = request()->param();

        $data = GameKindItem::withJoin('roomInfo')->where('GameKindItem.Nullity', 0)->select()->toArray();
        $stockList = [];
        if ($data) {
            foreach ($data as $val) {
                $KindID = $val['KindID'];
                $GameName = $val['KindName'];
                $ServerID = $val['roomInfo']['ServerID'];
                $RoomName = $val['roomInfo']['ServerName'];
                $key = "Stock:" . $KindID . ':' . $ServerID;
                $stock = Cache::get($key) ?? 0;
                $Stock = CommonService::dbToShow($stock);
                $info = [
                    'KindID' => $KindID,
                    'ServerID' => $ServerID,
                    'GameName' => $GameName,
                    'RoomName' => $RoomName,
                    'Stock' => $Stock
                ];
                $stockList[] = $info;
            }
        }
//        $stockList = [];
//        $keys = Cache::keys('Stock:*');
//        if ($keys) {
//            foreach ($keys as $var => $key) {
//                $data = Cache::get($key);
//                $column = explode(':', $key);
//                $game = GameKindItem::getGame($column[1]) ?? '';
//                $room = GameRoomInfo::getRoom($column[2]) ?? '';
//                if ($game && $room) {
//                    $info = [
//                        'KindID' => $column[1],
//                        'ServerID' => $column[2],
//                        'GameName' => $game,
//                        'RoomName' => $room,
//                        'Stock' => CommonService::dbToShow($data)
//                    ];
//                }
//
//                $stockList[] = $info;
//
//            }
//        }
        //获取数据总数
//        $count = $this->model->where($map)->count();
        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $stockList,
//            "count" => count($stockList),
        );
        return $message;
    }

    /**
     * 添加或编辑
     * @return array
     * @author tangsan
     */
    public function edit()
    {
        $data = request()->param();

        $KindID = $data['KindID'] ?? 0;
        $ServerID = $data['ServerID'] ?? 0;
        $Stock = $data['Stock'];
        $error = '';
        if (!$KindID) {
            $error = '参数错误！';
        }
        if (!$ServerID) {
            $error = '参数错误！';
        }
        $res = Cache::set('Stock:' . $KindID . ':' . $ServerID, CommonService::InsertToDb($Stock));

//        RecordModificationStock::
        $recordModel = new RecordModificationStock();

        $recordModel->ServerID = $ServerID;
        $recordModel->ModifyStock = $Stock;
        $recordModel->UserID =$this->userId;
        $recordModel->save();

        //添加库存记录

        if ($res) {
            return message();
        }
        return message($error, false);
    }

}